package com.superman.supermarket.client.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.event.ToastEvent;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.mvpframe.client.widget.mescroll.UpAndDownCallBack;
import com.superman.supermarket.client.event.CollectionPageEvent;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.view.collection.CollectionView;
import com.superman.supermarket.client.view.collection.CollectionView.CollectionViewPresenter;

/**
 * 收藏
 * 
 * @author superman
 * @version 2018年8月2日下午5:18:52
 */
public class CollectionActivity extends ShopAbstractActivity implements UpAndDownCallBack, CollectionViewPresenter {

	@Inject
	CollectionView collectionView;

	@Override
	public void clean() {
		postman.post("favorite/clean", null, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				collectionView.getPullToRefreshView().clearListAndResetScroll();
			}
		});
	}

	private void deleteCollection(String spuId) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<String> spus = new ArrayList<String>();
		spus.add(spuId);
		map.put("SPU_IDS", spus);
		postman.post("favorite/delete", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onBusinessOK(String response) {
				eventBus.fireEvent(new ToastEvent("已删除收藏"));
				collectionView.getPullToRefreshView().clearListAndResetScroll();
			}
		});
	}

	@Override
	public void getListDataByPage(int page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("PAGE_INDEX", page);
		postman.post("favorite/list", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				collectionView.getPullToRefreshView().endBySize(XJsonUtil.getList(response), XJsonUtil.getCount(response));
			}
		});
	}

	@Override
	public IPageView getView() {
		return collectionView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		return this;
	}

	@Override
	public void onActivity(EventBus eventBus) {
		eventBus.addHandler(CollectionPageEvent.TYPE, new CollectionPageEvent.CollectionPageEventHandler() {

			@Override
			public void delete(String spuId) {
				deleteCollection(spuId);
			}
		});
	}

	@Override
	public void popToRefresh() {
		collectionView.getPullToRefreshView().clearListAndResetScroll();
	}

	@Override
	public void popToRefresh(Object contextData) {
		collectionView.getPullToRefreshView().clearListAndResetScroll();
	}

	@Override
	public void pushToRefresh() {
		collectionView.getPullToRefreshView().initMeScroll(this, "1.2rem", "1.333333rem;");
	}

}
